package com.icez.base_util_library

import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.*
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.map

/**
 *  数据储存工具类
 * 
 * @author Icez
 */
class DateStoreUtil {


    companion object {
        val instance: DateStoreUtil by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            DateStoreUtil()
        }

        var mDataStore: DataStore<Preferences>? = null
    }

    /**
     *  初始化
     *  使用API之前请先初始化,请在application中初始化
     * @author Icez
     * @param dataStore 库  val dataStore: DataStore<Preferences> by preferencesDataStore(name = "settings")
     */
    fun init(dataStore: DataStore<Preferences>?) {
        mDataStore = dataStore
    }

    /**
     *  保存数据
     *  dataKey
     * @author Icez
     * @param dataKey 保存的key
     * @param data 保存的数据
     */
    suspend fun <T> saveDataForPreferences(dataKey: String, data: T) {
        if(mDataStore==null){
            throw RuntimeException("请在Application初始化")
        }
        try {
            var key: Preferences.Key<*>? = null
            when (data) {
                is Int -> {
                    key = intPreferencesKey(dataKey)
                }
                is Long -> {
                    key = longPreferencesKey(dataKey)
                }
                is Double -> {
                    key = doublePreferencesKey(dataKey)
                }
                is Float -> {
                    key = floatPreferencesKey(dataKey)
                }
                is String -> {
                    key = stringPreferencesKey(dataKey)
                }
                else ->{
                    throw RuntimeException("类型不支持")
                }
            }
            mDataStore?.edit {
                it[key as Preferences.Key<T>] = data
            }
        } catch (ex: Exception) {

        }

    }

    /**
     *  通过key获取指定的数据
     * 
     * @author Icez
     */
    suspend inline fun <reified T> getSaveDataForPreferances(dataKey: String): T? {
        if(mDataStore==null){
            throw RuntimeException("请在Application初始化")
        }
        try {
            var key: Preferences.Key<*>? = null
            when (T::class.java) {
                Int::class.java -> {
                    key = intPreferencesKey(dataKey)
                }
                Long::class.java -> {
                    key = longPreferencesKey(dataKey)
                }
                Double::class.java -> {
                    key = doublePreferencesKey(dataKey)
                }
                Float::class.java -> {
                    key = floatPreferencesKey(dataKey)
                }
                String::class.java -> {
                    key = stringPreferencesKey(dataKey)
                }
                else ->{
                    throw RuntimeException("类型不支持")
                }
            }

            var value: T? = null
            mDataStore?.data?.map {
                value = it[key as Preferences.Key<T>]
            }?.collect()

            return value
        } catch (ex: Exception) {
            return null
        }
    }


}